import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';


class DialogWidget extends Dialog {
  final String title; //标题
  final String content; //内容
  final String cancelText; //是否需要"取消"按钮
  final String confirmText; //是否需要"确定"按钮
  final Function cancelFun; //取消回调
  final Function confirmFun; //确定回调

  DialogWidget({
    Key key,
    @required this.title,
    @required this.content,
    this.cancelText,
    this.confirmText,
    @required this.cancelFun,
    this.confirmFun
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(15),
      child: Material(
        type: MaterialType.transparency,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              decoration: ShapeDecoration(
                color: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(
                    Radius.circular(10),
                  ),
                ),
              ),
              margin: EdgeInsets.all(15),
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.all(10),
                    child: Center(
                      child:  Text(title, style: TextStyle(color: Color(0xff666666))),
                    ),
                  ),
                  Container(
                    color: Color(0xffffffff),
                    height: 1.0,
                  ),
                  Container(
                    constraints: BoxConstraints(minHeight: 100),
                    child: Padding(
                      padding: const EdgeInsets.all(12.0),
                      child: IntrinsicHeight(
                        child:  Text(content, style: TextStyle(color: Color(0xff666666))),
                      ),
                    ),
                  ),
                  Container(
                    color: Color(0xffeeeeee),
                    height: 1.0,
                  ),
                  this._buildBottomButtonGroup()
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _buildBottomButtonGroup() {
    var widgets = <Widget>[];
    if (cancelText != null && cancelText.isNotEmpty) widgets.add(_buildBottomCancelButton());
    if (confirmText != null && confirmText.isNotEmpty && confirmText != null && confirmText.isNotEmpty) widgets.add(_buildBottomOnline());
    if (confirmText != null && confirmText.isNotEmpty) widgets.add(_buildBottomPositiveButton());

    return Flex(
      direction: Axis.horizontal,
      children: widgets,
    );
  }

  Widget _buildBottomOnline() {
    return Container(
      color: Color(0xffeeeeee),
      height: 38,
      width: 1,
    );
  }

  Widget _buildBottomCancelButton() {
    return Flexible(
      fit: FlexFit.tight,
      child: FlatButton(
        onPressed: this.cancelFun,
        child:  Text(cancelText, style: TextStyle(color: Color(0xff666666))),
      ),
    );
  }

  Widget _buildBottomPositiveButton() {
    return Flexible(
      fit: FlexFit.tight,
      child: FlatButton(
        onPressed: this.confirmFun,
        child:  Text(confirmText, style: TextStyle(color: Color(0xff666666))),
      ),
    );
  }

  /// 下载安卓更新包
//  Future<File> downloadAndroid(String url) async {
//    /// 创建存储文件
//    Directory storageDir = await getExternalStorageDirectory();
//    String storagePath = storageDir.path;
//    File file = new File('$storagePath/${Config.APP_NAME}v${_version}.apk');
//
//    if (!file.existsSync()) {
//      file.createSync();
//    }
//
//    try {
//      /// 发起下载请求
//      Response response = await Dio().get(url,
//          onReceiveProgress: showDownloadProgress,
//          options: Options(
//            responseType: ResponseType.bytes,
//            followRedirects: false,
//          ));
//      file.writeAsBytesSync(response.data);
//      return file;
//    } catch (e) {
//      print(e);
//    }
//  }

  /// 安装apk
//  Future<Null> installApk(String url) async {
//    File _apkFile = await downloadAndroid(url);
//    String _apkFilePath = _apkFile.path;
//
//    if (_apkFilePath.isEmpty) {
//      print('make sure the apk file is set');
//      return;
//    }
//
//    InstallPlugin.installApk(_apkFilePath, Config.APP_ID)
//        .then((result) {
//      print('install apk $result');
//    }).catchError((error) {
//      print('install apk error: $error');
//    });
//  }
}
